package com.bang.rabbitmq.consumer.listener;

import com.bang.rabbitmq.consumer.constants.QueueConstants;
import com.rabbitmq.client.Channel;
import org.springframework.messaging.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Auther: Bang
 * @Date: 2019/11/3 21:24
 * @Description:
 */
@Component
public class RabbitConsumer {

    @RabbitListener(
            bindings = {
            @QueueBinding(
                    value = @Queue(value = QueueConstants.TEST_DIRECT_QUEUE, durable = "true"),
                    exchange = @Exchange(value = QueueConstants.TEST_DIRECT_EXCHANGE,
                            durable = "true", type = "direct", ignoreDeclarationExceptions = "true"),
                    key = QueueConstants.TEST_DIRECT_ROUTE_KEY
            )
    })
    @RabbitHandler
    public void handleDirect(Message message, Channel channel) {
        System.err.println("--------------------------------------");
        System.err.println("消费端Payload: " + message.getPayload());
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        //手工ACK
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitHandler
    @RabbitListener(queues = QueueConstants.TEST_TOPIC_QUEUE_HELLO)
    public void handleHello(Message message, Channel channel) {
        System.err.println("--------------------------------------");
        System.err.println("消费端Payload: " + message.getPayload());
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        //手工ACK
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();

        }
    }

    @RabbitHandler
    @RabbitListener(queues = QueueConstants.TEST_TOPIC_QUEUE_WORLD)
    public void handleWorld(Message message, Channel channel) {
        System.err.println("--------------------------------------");
        Object payload = message.getPayload();
        System.err.println("消费端Payload: " + payload);
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        //手工ACK
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitHandler
    @RabbitListener(queues = QueueConstants.FANOUT_QUEUE1)
    public void queue1(Message message, Channel channel) {
        System.err.println("--------------------------------------");
        Object payload = message.getPayload();
        System.err.println("消费端Payload: " + payload);
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        //手工ACK
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitHandler
    @RabbitListener(queues = QueueConstants.FANOUT_QUEUE2)
    public void queue2(Message message, Channel channel) {
        System.err.println("--------------------------------------");
        Object payload = message.getPayload();
        System.err.println("消费端Payload: " + payload);
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        //手工ACK
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RabbitHandler
    @RabbitListener(queues = QueueConstants.FANOUT_QUEUE3)
    public void queue3(Message message, Channel channel) {
        System.err.println("--------------------------------------");
        Object payload = message.getPayload();
        System.err.println("消费端Payload: " + payload);
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        //手工ACK
        try {
            channel.basicAck(deliveryTag, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
